package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.interceptor.UserHolder;
import cn.zcscloud.mall.mbg.dto.AdminMenuDto;
import cn.zcscloud.mall.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName: LoginController
 * Description:
 * date: 2022/3/31 10:57
 *
 * @author zjf95
 * @since JDK 1.8
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * Description: 登录以后返回token
     *
     * @author: zjf95
     * @date: 2022/3/31 16:24
     * @param:
     * @return:
     */
    @PostMapping("/login")
    public R login(@RequestBody Map map) {

        String password = (String) map.get("password");
        String username = (String) map.get("username");

        Map resMap = loginService.login(username, password);

        return R.success("操作成功", resMap);

    }

    @GetMapping("/info")
    public R info() {

        Long userId = UserHolder.getUserId();

        AdminMenuDto adminMenuDto = loginService.findByUserId(userId);

        return R.success("操作成功", adminMenuDto);
    }

    @PostMapping("/logout")
    public R logout() {

        loginService.logout();

        return R.success("操作成功", null);
    }
}
